/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.manage.theme.controller;

import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.annotion.Permission;
import vip.xiaonuo.core.context.login.LoginContextHolder;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.pojo.login.SysLoginUser;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.modular.manage.biindextype.entity.BiIndexType;
import vip.xiaonuo.modular.manage.biindextype.service.BiIndexTypeService;
import vip.xiaonuo.modular.entitys.Tree;
import vip.xiaonuo.modular.manage.theme.entity.BiThemeIndex;
import vip.xiaonuo.modular.manage.theme.param.ThemeIndexParam;
import vip.xiaonuo.modular.manage.theme.param.ThemeParam;
import vip.xiaonuo.modular.manage.theme.service.ThemeIndexService;
import vip.xiaonuo.modular.manage.theme.service.ThemeService;
import org.springframework.validation.annotation.Validated;
import vip.xiaonuo.util.BIndexTypeHandler;
import vip.xiaonuo.util.ThemeIndexTypeHandler;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 主题管理控制器
 *
 * @author cy
 * @date 2022-07-28 19:43:15
 */
@RestController
public class ThemeController {

    @Resource
    private ThemeService themeService;
    @Resource
    private ThemeIndexService themeIndexService;
    @Resource
    private BiIndexTypeService biIndexTypeService;

    /**
     * 查询主题管理
     *
     * @author cy
     * @date 2022-07-28 19:43:15
     */
    @Permission
    @GetMapping("/theme/page")
    @BusinessLog(title = "主题管理_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(ThemeParam themeParam) {
        return new SuccessResponseData(themeService.page(themeParam));
    }


    /**
     * 获取全部节点并构造成Tree返回
     *
     * @author cy
     * @date 2022-07-28 19:43:15
     */
    @Permission
    @GetMapping("/theme/allTree")
    @BusinessLog(title = "主题管理_获取全部节点", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData allTree() {
        List<BiIndexType> indexTypes = biIndexTypeService.list();
        BIndexTypeHandler handler = new ThemeIndexTypeHandler();
        List<Tree> list = handler.allTree(indexTypes);
        SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
        System.out.println("用户id:"+sysLoginUser.getId());
        return new SuccessResponseData(list);
    }

    /**
     * 获取主题相关指标列表
     * @author: cy
     * @Date: 2022/8/1 19:01
     **/
    @Permission
    @GetMapping("/theme/themeTree/{id}")
    @BusinessLog(title = "主题管理_获取主题相关指标列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData themeTree(@PathVariable("id") Long id) {
        ThemeIndexParam themeIndexParam = new ThemeIndexParam();
        themeIndexParam.setThemeId(id);
        List<BiThemeIndex> themeIndexs = themeIndexService.list(themeIndexParam);
        Long[] nodes = themeService.themeTree(themeIndexs);
        return new SuccessResponseData(nodes);
    }

    /**
     * 获取主题相关指标列表
     * @author: cy
     * @Date: 2022/8/1 19:01
     **/
    @Permission
    @GetMapping("/theme/poi/themeTree/{id}")
    @BusinessLog(title = "主题管理_获取poi主题相关指标列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData poiThemeTree(@PathVariable("id") Long id) {
        ThemeIndexParam themeIndexParam = new ThemeIndexParam();
        themeIndexParam.setThemeId(id);
        List<BiThemeIndex> themeIndexs = themeIndexService.list(themeIndexParam);
//        Long[] nodes = themeService.themeTree(themeIndexs);
        List<String> nodes = themeIndexs.stream().filter(el->el.getPoiId() != null)
                .map(el -> String.valueOf(el.getPoiId())).collect(Collectors.toList());
        return new SuccessResponseData(nodes);
    }

    /**
     * 添加主题管理
     *
     * @author cy
     * @date 2022-07-28 19:43:15
     */
    @Permission
    @PostMapping("/theme/add")
    @BusinessLog(title = "主题管理_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(ThemeParam.add.class) ThemeParam themeParam){
        themeService.add(themeParam);
        return new SuccessResponseData();
    }

    /**
     * 删除主题管理，可批量删除
     *
     * @author cy
     * @date 2022-07-28 19:43:15
     */
    @Permission
    @PostMapping("/theme/delete")
    @BusinessLog(title = "主题管理_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(ThemeParam.delete.class) List<ThemeParam> themeParamList) {
            themeService.delete(themeParamList);
        return new SuccessResponseData();
    }

    /**
     * 主题启用/停用
     *
     * @author cy
     * @date 2022-07-28 19:43:15
     */
    @Permission
    @PostMapping("/theme/status")
    @BusinessLog(title = "主题启用/停用", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData status(@RequestBody ThemeParam themeParam) {
        themeService.status(themeParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑主题管理
     *
     * @author cy
     * @date 2022-07-28 19:43:15
     */
    @Permission
    @PostMapping("/theme/edit")
    @BusinessLog(title = "主题管理_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(ThemeParam.edit.class) ThemeParam themeParam) {
            themeService.edit(themeParam);
        return new SuccessResponseData();
    }

    /**
     * 查看主题管理
     *
     * @author cy
     * @date 2022-07-28 19:43:15
     */
    @Permission
    @GetMapping("/theme/detail")
    @BusinessLog(title = "主题管理_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(ThemeParam.detail.class) ThemeParam themeParam) {
        return new SuccessResponseData(themeService.detail(themeParam));
    }

    /**
     * 主题管理列表
     *
     * @author cy
     * @date 2022-07-28 19:43:15
     */
    @Permission
    @GetMapping("/theme/list")
    @BusinessLog(title = "主题管理_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(ThemeParam themeParam) {
        return new SuccessResponseData(themeService.list(themeParam));
    }

    /**
     * 导出主题管理
     *
     * @author cy
     * @date 2022-07-28 19:43:15
     */
    @Permission
    @GetMapping("/theme/export")
    @BusinessLog(title = "主题管理_导出", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void export(ThemeParam themeParam) {
        themeService.export(themeParam);
    }

}
